package com.thoughtservice.jspdoc.processors;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtservice.jspdoc.JSPFileList;
import com.thoughtservice.jspdoc.JSPPage;
import com.thoughtservice.jspdoc.RE;
import com.thoughtservice.jspdoc.RESyntaxException;
import com.thoughtservice.jspdoc.utils.ProcessorUtils;

public class ProcessDynamicIncludes implements DocProcessor{

	private final static Logger LOG = LoggerFactory.getLogger(ProcessDynamicIncludes.class);
	
	public boolean process(JSPPage p, String pc, JSPFileList fileList) throws RESyntaxException {
		LOG.trace("Entering {}", "ProcessDynamicIncludes");
		RE re;
		int index;
		//		re = new RE("<jsp:include[:space:]*page=\"[:print:]*\"[:space:]*flush[:space:]*=[:space:]*\"true\"[:space:]*>");
		re = new RE("<jsp:include[:space:]*page=\"\\S*\"[:space:]*flush[:space:]*=[:space:]*\"true\"[:space:]*>");
		index = 0;
		while (re.match(pc, index)) {
			//			System.out.println(re.getParen(0));
			String link = re.getParen(0);
			link = ProcessorUtils.figureLink(link, p, JSPPage.D_INCLUDES);
			JSPPage dest = ProcessorUtils.getPage(link, fileList);
			if (dest != null)
				dest.addConnection(JSPPage.D_INCLUDEDIN, p.getName());
			index = re.getParenEnd(0);
		}
		LOG.trace("Exiting {}", "ProcessDynamicIncludes");
	
		return false;
	}
}
